﻿namespace Windows32
{
    using System;

    public class Win32APIException : Exception
    {
        private int intLastDllError;
        private int intReturnValue;
        private string strAPIName;
        private string strErrorMsg;

        public Win32APIException()
        {
            this.intLastDllError = 0;
            this.intReturnValue = 0;
            this.strAPIName = "";
            this.strErrorMsg = "";
        }

        public Win32APIException(string vAPIName, int vReturnValue)
        {
            this.intLastDllError = 0;
            this.intReturnValue = 0;
            this.strAPIName = "";
            this.strErrorMsg = "";
            this.strAPIName = vAPIName;
            this.intLastDllError = (int) Kernel32.GetLastError();
            this.intReturnValue = vReturnValue;
        }

        public Win32APIException(string vAPIName, int vReturnValue, int vDllError)
        {
            this.intLastDllError = 0;
            this.intReturnValue = 0;
            this.strAPIName = "";
            this.strErrorMsg = "";
            this.strAPIName = vAPIName;
            this.intLastDllError = vDllError;
            this.intReturnValue = vReturnValue;
        }

        public Win32APIException(string vAPIName, int vReturnValue, int vDllError, string msg)
        {
            this.intLastDllError = 0;
            this.intReturnValue = 0;
            this.strAPIName = "";
            this.strErrorMsg = "";
            this.strAPIName = vAPIName;
            this.intLastDllError = vDllError;
            this.intReturnValue = vReturnValue;
            this.strErrorMsg = msg;
        }

        public override string ToString()
        {
            return string.Concat(new object[] { "Win32APIException:调用Win32API函数", this.strAPIName, " 错误，返回:", this.intReturnValue, " 系统错误号", this.intLastDllError, Kernel32.FormatErrorMessage(this.intLastDllError), " 错误信息: ", this.strErrorMsg, "\r\n", this.StackTrace });
        }

        public string APIName
        {
            get
            {
                return this.strAPIName;
            }
        }

        public string ErrorMsg
        {
            get
            {
                return this.strErrorMsg;
            }
            set
            {
                this.strErrorMsg = value;
            }
        }

        public int LastDllError
        {
            get
            {
                return this.intLastDllError;
            }
        }

        public int ReturnValue
        {
            get
            {
                return this.intReturnValue;
            }
        }
    }
}

